/* model */
import Field from '@model/entity/Field'
import { ConnectorBizTypeEnum, ConnectorModuleComponentNameEnum } from '@src/modules/connector/model'
/* mixin */
import { ConnectorModuleConnectorCreateMixin } from '@src/modules/connector/mixin'
/* vue */
import Component from 'vue-class-component'
/* util */
import Log from '@src/util/log'
import { initialize } from '@src/component/form/util'
import { packToForm } from '@src/modules/task/util/task'
import { isArray, isNotEmpty, isObject } from '@src/util/type'
import { getConnectorCreateDataSessionKeyByBizType } from '@src/modules/connector/util'

@Component({ 
  name: ConnectorModuleComponentNameEnum.ConnectorModuleConnectorCreateAddonsMixin,
})
export default class ConnectorModuleConnectorCreateAddonsMixin extends ConnectorModuleConnectorCreateMixin {
  
  /* 连接器新建数据 session 键 */
  public connectorCreateDataSessionKey = getConnectorCreateDataSessionKeyByBizType(ConnectorBizTypeEnum.Addons)
  
  /* 混入的组件需要重写此属性 */
  private form: Record<string, any> = {}
  
  /** 
   * @description 连接器新建数据处理
  */
  public connectorInsertSelectCallDataHandler(data: Record<string, any>) {
    
    
    const packData = {
      ...data
    }

    
    const value = initialize(this.fields, packData)

    if(value?.additionalCustomer) {
      value.additionalCustomer = Array.isArray(value.additionalCustomer) ? value.additionalCustomer : [value.additionalCustomer]
      this.$nextTick(()=> {
        this.$refs.searchCustomerRef.options = value?.['additionalCustomer'] || []
      })
    }
    
    setTimeout(() => {
      
      const newValue = {
        ...this.form,
        ...value
      }
      
      Log.info(newValue, 'newValue', this.connectorInsertSelectCallDataHandler.name)
      
      Object.keys(newValue).forEach(key => {
        const value = newValue[key]
        this.$set(this.form, key, value)
      })
      
    }, 300)
    
  }
  
  /**
   * @description 消息处理后的事件
  */
  public onFrameMessageAfterHandler() {
    setTimeout(() => {
      this.fetchConnectorInsertSelectCall()
    }, 300)
  }
  
}

